package cn.dakaqi.entities;

import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chunyang on 2016/4/29.
 */
@Entity
@Cacheable
@Table(name = "U_INDEX_DATA")
public class IndexData extends IdEntity
{
    SimpleDateFormat DefaultTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");


    private String type;    //分类,
    private int delStatus;//是否被删除
    private int top;//是否被置顶
    private Date createTime;//创建日期
    private int display;//是否显示
    private String city;    //招募的地点
    private String job;    //招募所需的职业
    private String serviceField;    //招募的服务领域,
    private String skill;    //招募所需的个人技能
    private SysTopic sysTopic;
    private ActivityRecruit activityRecruit;
    private GroupRecruit groupRecruit;
    private Date stopTime;
    private int stopStatus;

    public IndexData(Long id)
    {
        this.id = id;
    }

    public IndexData()
    {
    }

    public int getStopStatus()
    {
        return stopStatus;
    }

    public void setStopStatus(int stopStatus)
    {
        this.stopStatus = stopStatus;
    }

    public Date getStopTime()
    {
        return stopTime;
    }

    public void setStopTime(Date stopTime)
    {
        this.stopTime = stopTime;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getJob()
    {
        return job;
    }

    public void setJob(String job)
    {
        this.job = job;
    }

    public String getServiceField()
    {
        return serviceField;
    }

    public void setServiceField(String serviceField)
    {
        this.serviceField = serviceField;
    }

    public String getSkill()
    {
        return skill;
    }

    public void setSkill(String skill)
    {
        this.skill = skill;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public int getDelStatus()
    {
        return delStatus;
    }

    public void setDelStatus(int delStatus)
    {
        this.delStatus = delStatus;
    }

    public int getTop()
    {
        return top;
    }

    public void setTop(int top)
    {
        this.top = top;
    }

    // 设定JSON序列化时的日期格式
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public int getDisplay()
    {
        return display;
    }

    public void setDisplay(int display)
    {
        this.display = display;
    }

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "SYS_TOPIC_ID")
    public SysTopic getSysTopic()
    {
        return sysTopic;
    }

    public void setSysTopic(SysTopic sysTopic)
    {
        this.sysTopic = sysTopic;
    }

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "ACTIVITY_RECRUIT_ID")
    public ActivityRecruit getActivityRecruit()
    {
        return activityRecruit;
    }

    public void setActivityRecruit(ActivityRecruit activityRecruit)
    {
        this.activityRecruit = activityRecruit;
    }

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "GROUP_RECRUIT_ID")
    public GroupRecruit getGroupRecruit()
    {
        return groupRecruit;
    }

    public void setGroupRecruit(GroupRecruit groupRecruit)
    {
        this.groupRecruit = groupRecruit;
    }

    /***************************************************************/
    private String img;     //封面图
    private String title;   //标题
    private Long objectId;    //对象类型ID
    private int browses;//浏览量
    private int participants;//参与人数
    private String nickName;//话题发布者昵称
    private int status;//进行状态
    private long days;//招募剩余的天数
    private String demo;//话题简介
    private String orgName;//组织名称

    @Transient
    public int getStatus()
    {
        if(type.equals("GroupRecruit")&& null != groupRecruit)
        {
            status = groupRecruit.getStatus();
        }
        else if(type.equals("ActivityRecruit")&& null != activityRecruit)
        {
            status =  activityRecruit.getStatus();
        }
        return status;
    }

    @Transient
    public String getDemo()
    {
        if(type.equals("SysTopic") && null != sysTopic)
        {
            demo = sysTopic.getIntro();
        }
        else
            demo = "";

        return demo;
    }

    @Transient
    public String getOrgName()
    {
        if(type.equals("GroupRecruit")&& null != groupRecruit)
            orgName = groupRecruit.getGroup().getName();
        else if(type.equals("ActivityRecruit")&& null != activityRecruit)
            orgName = activityRecruit.getActivity().getGroup().getName();

        return orgName;
    }

    @Transient
    public long getDays()
    {
        try
        {
            if(type.equals("GroupRecruit")&& null != groupRecruit)
            {
                //if(groupRecruit.getStatus() != DKQConstant.PROCEED_END)
                    days = DateUtil.countDay(new Date(), groupRecruit.getStopApplyTime());
            }
            else if(type.equals("ActivityRecruit")&& null != activityRecruit)
            {
                //if(activityRecruit.getStatus() != DKQConstant.PROCEED_END)
                {
                    Date endDate = DefaultTimeFormatter.parse(activityRecruit.getActivity().getEndTime());
                    days = DateUtil.countDay(new Date(), endDate);
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return days;
    }
    @Transient
    public String getNickName()
    {
        if(type.equals("SysTopic")&& null != sysTopic)
            nickName = sysTopic.getPromulgator();
        return nickName;
    }
    @Transient
    public int getBrowses()
    {
        if(type.equals("SysTopic")&& null != sysTopic)
            browses = sysTopic.getBrowses();
        if(type.equals("GroupRecruit")&& null != groupRecruit)
            browses = groupRecruit.getBrowses();
        if(type.equals("ActivityRecruit")&& null != activityRecruit)
            browses = activityRecruit.getBrowses();

        return browses;
    }
    @Transient
    public int getParticipants()
    {
        if(type.equals("SysTopic")&& null != sysTopic)
            participants = sysTopic.getParticipants();
        else if(type.equals("GroupRecruit")&& null != groupRecruit)
            participants = groupRecruit.getApplys();
        else if(type.equals("ActivityRecruit")&& null != activityRecruit)
            participants = activityRecruit.getApplys();
        return participants;
    }
    @Transient
    public String getImg()
    {
        if(type.equals("SysTopic")&& null != sysTopic)
            img = sysTopic.getBackground();
        else if(type.equals("GroupRecruit")&& null != groupRecruit)
            img = groupRecruit.getImg();
        else if(type.equals("ActivityRecruit")&& null != activityRecruit)
            img = activityRecruit.getImg();
        return img;
    }
    @Transient
    public String getTitle()
    {
        if(type.equals("SysTopic") && null != sysTopic)
        {
            title = sysTopic.getTitle();
        }
        else if(type.equals("GroupRecruit") && null != groupRecruit)
            title = groupRecruit.getTitle();
        else if(type.equals("ActivityRecruit") && null != activityRecruit)
            title = activityRecruit.getActivity().getName();
        return title;
    }
    @Transient
    public Long getObjectId()
    {
        if(type.equals("SysTopic")&& null != sysTopic)
            objectId = sysTopic.getId();
        else if(type.equals("GroupRecruit")&& null != groupRecruit)
            objectId = groupRecruit.getId();
        else if(type.equals("ActivityRecruit")&& null != activityRecruit)
            objectId = activityRecruit.getId();
        return objectId;
    }

    /***************************************************************/
}
